<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="fl">
        <span style="font-size: 20pt;color: black">比赛人员管理</span>
        <span style="font-size: 15pt;color:gray"> 管理员列表</span>
      </div>
      <div class="fr">
          <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">添加 </el-button>
        <el-button class="filter-item" style="margin-left: 10px;" @click="handleDelete(multipleSelection)" type="danger" icon="el-icon-delete">删除</el-button>
      </div>
    </div>
    <el-table class="tqd_table" :key='tableKey' :data="list" v-loading="listLoading"
              @selection-change="handleSelectionChange" border fit highlight-current-row
              style="width: 100%;" >
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <el-table-column width="120" prop="matchAdminId" label="序号" show-overflow-tooltip></el-table-column>
      <el-table-column label="登录账号" prop="account" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="名称" prop="nickName" show-overflow-tooltip>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/sys/match/referee?id='+listQuery.matchId+'&adminId='+scope.row.matchAdminId">
            <el-button type="primary" size="mini">裁判管理</el-button>
          </router-link>
          <el-button size="mini" type="danger" @click="handleDelete([scope.row.matchAdminId])">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="listQuery.page" :page-sizes="[15,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="添加管理员(裁判编制，与建赛设置一致)" :visible.sync="dialogFormVisible" width="550px" :close-on-click-modal="false">
      
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="70px"
               style='width: 100%; '>
        <el-form-item prop="account" label="账号">
          <el-input type="input" v-model="temp.account" auto-complete="off" ></el-input>
        </el-form-item>
        <el-form-item prop="nickName" label="名称">
          <el-input type="input" v-model="temp.nickName" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="createData">确定</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<script>
  import { getAdminList, saveMatchAdmin } from '@/api/match'
  import waves from '@/directive/waves' // 水波纹指令
  import { deleteUser } from '@/api/user'
  import { showMsg } from '@/utils'

  export default {
    name: 'complexTable',
    directives: {
      waves
    },
    data() {
      return {
        multipleSelection: [],
        tableKey: 0,
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          matchId: null,
          pageNum: 1,
          pageSize: 15,
          userId: null
        },
        temp: {
          matchId: null,
          nickName: null,
          account: 123456
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '修改',
          create: '添加'
        },
        dialogPvVisible: false,
        pvData: [],
        rules: {
          nickName: [
            {
              required: true,
              message: '必填',
              trigger: 'blur'
            }
          ]
        },
        downloadLoading: false
      }
    },
    created() {
      this.listQuery.matchId = this.$route.query.id
      this.listQuery.userId = this.$route.query.userId
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        getAdminList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data.total
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.pageNum = 1
        this.getList()
      },
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val
        this.getList()
      },
      handleModifyStatus(row, status) {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        row.status = status
      },
      resetTemp() {
        this.temp = {
          matchId: null,
          nickName: null,
          account: null
        }
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.temp.matchId = parseInt(this.$route.query.id)
            saveMatchAdmin(this.temp).then(res => {
              if (res.data.code === 0) {
                showMsg(res.data.message, 1)
                this.dialogFormVisible = false
                this.getList()
              } else {
                showMsg(res.data.message, 2)
              }
            })
          }
        })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.temp.timestamp = new Date(this.temp.timestamp)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
            getAdminList(tempData).then(() => {
              for (const v of this.list) {
                if (v.id === this.temp.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.temp)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = []
        for (var i in val) {
          this.multipleSelection.push(val[i].matchAdminId)
        }
      },
      handleDelete(idList) {
        if (idList.length === 0) {
          showMsg('请至少选择一条数据', 2)
          return false
        }
        this.$confirm('您确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteUser(idList).then(res => {
            if (res.data.code === 0) {
              showMsg(res.data.message, 1)
              this.getList()
            } else {
              showMsg(res.data.message, 2)
            }
          })
        }).catch(() => {
          // console.log('===')
        })
      }
    }
  }
</script>
